import ajax from '@/api'
/**
 *
 * @param {*} url 打开的地址
 * @param {*} param 向新页面传递的参数
 */
const openView = (url, param = {}) => {
	let params = Object.keys(param),
		arg = ''
	if (params.length) {
		arg = '?'
		params.forEach((item) => {
			arg += `${item}=${param[item]}&`
		})
	}
	arg = arg.substring(0, arg.length - 1)
	uni.reLaunch({
		url: `/pages${url}${arg}`
	})
}
/**
 *设置全局
 * @param {*} obj 设置的参数
 */
const setApp = () => {
	return new Promise((resolved, rejected) => {
		ajax
			.getStepInfo()
			.then((res) => {
				const app = getApp().globalData
				app['is_ab'] = res.is_ab
				app['step_normal'] = res.step_normal
				app['step_fast'] = res.step_fast
				resolved(app)
			})
			.catch((err) => {
				rejected(err)
			})
	})
}
//获取全局数据
const getapp = async (param = {}) => {
	const app = await setApp()
	const result = Object.prototype.toString.call(param).slice(8, -1) == 'Object' ? app : app[param]
	return result
}
//设置ab面
const setTabBar = async () => {
	const app = await getapp()
	let is_ab = ''
	let path = '/pages/my/index'
	if (app.step_fast == 5 && app.is_ab == 1) {
		is_ab = '/pages/financial/putSucceeA'
	}
	if (app.step_fast == 6 && app.is_ab == 1) {
		is_ab = '/pages/financial/resultA'
	}
	if ((app.step_fast == 5 || app.step_fast == 6) && app.is_ab == 0) {
		is_ab = '/pages/financial/putSucceeB'
	}
	if (app.is_ab == 1) {
		path = '/pages/my/myA'
	}
	//设置结果页
	uni.setTabBarItem({
		index: 2,
		pagePath: is_ab
	})
	uni.setTabBarItem({
		index: 4,
		pagePath: path
	})
	if (app.is_ab == 0) return
	;[0, 1].map((item) => {
		uni.setTabBarItem({
			index: item,
			visible: false
		})
	})
	uni.switchTab({
		url: is_ab
	})
}
export { openView, setTabBar, getapp }
